package cn.heycm.tenant.controller;

import cn.heycm.tenant.common.constant.TenantConstant;
import cn.heycm.tenant.user.UserService;
import jakarta.servlet.http.HttpServletRequest;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * @author hey
 * @version 1.0
 * @since 2024/8/25 13:01
 */
@RestController
@RequestMapping("/demo")
@AllArgsConstructor
@Slf4j
public class DemoController {

    private final UserService userService;

    /**
     * 测试不带 @Tenant 注解时，走默认租户
     *
     * @param userId
     * @return
     */
    @GetMapping("/defaultTenant/{userId}")
    public String defaultTenant(@PathVariable Integer userId) {
        return userService.defaultTenant(userId);
    }

    /**
     * 测试带 @Tenant 注解时，走固定租户
     *
     * @param userId
     * @return
     */
    @GetMapping("/fixedTenant/{userId}")
    public String fixedTenant(@PathVariable Integer userId) {
        return userService.fixedTenant(userId);
    }

    /**
     * 测试带 @Tenant 注解时，走请求头中的租户
     *
     * @param userId
     * @return
     */
    @GetMapping("/headerTenant/{userId}")
    public String headerTenant(@PathVariable Integer userId, HttpServletRequest request) {
        String header = request.getHeader(TenantConstant.HEADER);
        log.info("headerTenant: {}", header);
        return userService.headerTenant(userId);
    }

    /**
     * 测试事务
     *
     * @param name
     * @return
     */
    @PostMapping("/transaction")
    public String transaction(String name) {
        return userService.transaction(name);
    }
}
